"use strict";
cc._RF.push(module, '63db27lk3VDHpdtSekjBQ9G', 'GameDrag');
// scripts/GameDrag.ts

Object.defineProperty(exports, "__esModule", { value: true });
var Config_1 = require("./Config");
var Model_1 = require("./Model");
var MetaballMgr_1 = require("./MetaballMgr");
var WxUtil_1 = require("./WxUtil");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var GameDrag = /** @class */ (function (_super) {
    __extends(GameDrag, _super);
    function GameDrag() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.grac = null;
        _this.Cup = null;
        _this.dotLine = null;
        _this.waterPipe = null;
        _this.waterContainer = null;
        _this.metaBall = null;
        _this.ground = null;
        _this.Ball = null;
        _this.TopUI = null;
        _this.BottomUI = null;
        _this.Brick = null;
        _this.touchX = 0;
        _this.touchY = 0;
        _this.lock = false;
        _this.totalWater = 44;
        _this.waterCount = 0;
        _this.over = false;
        _this.waterClip = null;
        _this.waterSoundDelay = 0.2;
        _this.waterSoundDelayCount = 10;
        return _this;
    }
    ;
    ;
    ;
    ;
    GameDrag.prototype.onLoad = function () {
        Config_1.default.init();
        //开启物理效果
        cc.director.getPhysicsManager().enabled = true;
        cc.director.getPhysicsManager().gravity = new cc.Vec2(0, -800);
        // cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_jointBit | cc.PhysicsManager.DrawBits.e_shapeBit;
        this.draw = this.grac.getComponent(cc.Graphics);
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        this.createWorld();
        var top = cc.instantiate(this.TopUI);
        this.node.addChild(top);
        this.metaBall.getComponent(MetaballMgr_1.default).waters = this.waters;
        this.node.getChildByName('gameTip').active = Model_1.default.game.selectedLevel == 1;
    };
    GameDrag.prototype.createWorld = function () {
        var _this = this;
        //createCup        
        var level = Model_1.default.game.selectedLevel;
        var data = Config_1.default.dragLevels[level];
        var cupPos = data[0];
        var cup = cc.instantiate(this.Cup);
        cup.getComponent(cc.RigidBody).type = cc.RigidBodyType.Dynamic;
        cup.x = cupPos[0];
        cup.y = cupPos[1];
        this.node.addChild(cup);
        var cupData = Config_1.default.cupDatas[1];
        for (var i = 0; i < cupData.polygonColliderList.length; i++) {
            cup.addComponent(cc.PhysicsPolygonCollider);
        }
        //添加改变后，要用apply
        var allCollider = cup.getComponents(cc.PhysicsPolygonCollider);
        for (i = 0; i < allCollider.length; i++) {
            var tempC = allCollider[i];
            tempC.points = cupData.polygonColliderList[i];
            tempC.density = 2;
            tempC.apply();
        }
        this.cup = cup;
        this.dotLine.x = data[2][0];
        this.dotLine.y = data[2][1];
        this.waterPipe.x = data[1][0];
        this.waterPipe.y = data[1][1];
        //bricks
        for (var i = 3; i < data.length; i++) {
            var brickType = data[i][0];
            var brick = cc.instantiate(this.Brick);
            brick.x = data[i][1];
            brick.y = data[i][2];
            if (brickType == 1) //矩形
             {
                brick.setContentSize(cc.size(data[i][3], data[i][4]));
                brick.getComponent(cc.PhysicsBoxCollider).size = cc.size(cc.size(data[i][3], data[i][4]));
                brick.getComponent(cc.PhysicsBoxCollider).apply();
            }
            brick.angle = -data[i][5];
            this.node.addChild(brick);
        }
        this.ground.getComponent(cc.RigidBody).onBeginContact = function (contact, selfCollider, otherCollider) {
            _this.onBeginContact(contact, selfCollider, otherCollider);
        };
        this.waters = [];
    };
    GameDrag.prototype.onTouchStart = function (e) {
        if (this.lock)
            return;
        var pos = e.getLocation();
        pos = this.node.convertToNodeSpaceAR(pos);
        this.touchX = pos.x;
        this.touchY = pos.y;
        this.draw.moveTo(pos.x, pos.y);
    };
    GameDrag.prototype.onTouchMove = function (e) {
        if (this.lock)
            return;
        var pos = e.getLocation();
        pos = this.node.convertToNodeSpaceAR(pos);
        this.draw.clear();
        this.draw.moveTo(this.touchX, this.touchY);
        this.draw.lineTo(pos.x, pos.y);
        this.draw.stroke();
    };
    GameDrag.prototype.onTouchEnd = function (e) {
        if (this.lock)
            return;
        this.lock = true;
        this.draw.clear();
        var center = this.cup.getComponent(cc.RigidBody).getWorldCenter();
        var pos = e.getLocation();
        pos = this.node.convertToNodeSpaceAR(pos);
        var dx = pos.x - this.touchX;
        var dy = pos.y - this.touchY;
        //var angle=Math.atan2(dy,dx);
        var dis = Math.sqrt(dx * dx + dy * dy);
        var speed = 20 * dis;
        var v = cc.v2(dx / dis * speed, dy / dis * speed);
        this.cup.getComponent(cc.RigidBody).applyLinearImpulse(v, cc.v2(center.x, center.y - 30), true);
        var sign = dx < 0 ? 1 : -1;
        this.cup.getComponent(cc.RigidBody).applyAngularImpulse(dis * 1000 * sign, true);
        this.schedule(this.startCheck, 0.016);
    };
    GameDrag.prototype.start = function () {
        WxUtil_1.default.destroyBanner();
    };
    GameDrag.prototype.startCheck = function () {
        if (this.lock) {
            if (this.cup.x < -455 || this.cup.x > 830 || this.cup.y < -500) {
                this.gameLose();
                return;
            }
            if (this.cup.getComponent(cc.RigidBody).awake == false) {
                this.unschedule(this.startCheck);
                // if (Math.abs(this.cup.rotation) > 70) {
                //    this.gameLose()
                // } else {
                this.startDropWater();
                //  }
            }
        }
    };
    //开始滴水
    GameDrag.prototype.startDropWater = function () {
        console.log('开始创建水');
        this.schedule(this.createWater, 0.1);
    };
    GameDrag.prototype.createWater = function () {
        if (this.waterCount < this.totalWater) {
            this.waterCount++;
            var ball = cc.instantiate(this.Ball);
            ball.x = this.waterPipe.x + Math.random() * 8 - 16;
            ball.y = this.waterPipe.y + Math.random() * 5 - 75;
            // ball.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, -380)
            ball.getComponent(cc.PhysicsCircleCollider).density = 1;
            this.waterContainer.addChild(ball, -1);
            this.waters.push(ball);
            this.waterSoundDelayCount += 0.1;
            if (this.waterSoundDelayCount > this.waterSoundDelay) {
                this.waterSoundDelayCount = 0;
                cc.audioEngine.playEffect(this.waterClip, false);
            }
        }
        else {
            this.unschedule(this.createWater);
            this.schedule(this.checkWin, 0.1);
        }
    };
    GameDrag.prototype.checkWin = function () {
        if (this.over)
            return;
        var bol = true;
        for (var _i = 0, _a = this.waters; _i < _a.length; _i++) {
            var water = _a[_i];
            if (water.getComponent(cc.RigidBody).awake) {
                bol = false;
                break;
            }
        }
        if (bol) {
            this.gameWin();
        }
    };
    GameDrag.prototype.onBeginContact = function (contact, selfCollider, otherCollider) {
        if (selfCollider.node.name == 'ground' && otherCollider.node.name == 'Ball' && !this.over) {
            this.gameLose();
        }
    };
    GameDrag.prototype.showFace = function (type) {
        this.cup.getChildByName('facenormal').active = false;
        this.cup.getChildByName('facewin').active = false;
        this.cup.getChildByName('facelose').active = false;
        if (type == 2) {
            this.cup.getChildByName('facewin').active = true;
        }
        else if (type == 1) {
            this.cup.getChildByName('facelose').active = true;
        }
        else if (type == 0) {
            this.cup.getChildByName('facenormal').active = true;
        }
    };
    GameDrag.prototype.update = function (dt) {
        if (this.waterCount > 0) {
            this.metaBall.getComponent(MetaballMgr_1.default).draw();
        }
    };
    GameDrag.prototype.gameWin = function () {
        Model_1.default.game.dragLevel++;
        this.unscheduleAllCallbacks();
        this.over = true;
        Model_1.default.game.win = true;
        this.showFace(2);
        this.scheduleOnce(this.delayShowEnd, 1);
    };
    GameDrag.prototype.gameLose = function () {
        this.unscheduleAllCallbacks();
        this.over = true;
        Model_1.default.game.win = false;
        this.showFace(1);
        this.scheduleOnce(this.delayShowEnd, 1);
    };
    GameDrag.prototype.delayShowEnd = function () {
        cc.director.loadScene('gameEnd');
    };
    __decorate([
        property(cc.Node)
    ], GameDrag.prototype, "grac", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDrag.prototype, "Cup", void 0);
    __decorate([
        property(cc.Node)
    ], GameDrag.prototype, "dotLine", void 0);
    __decorate([
        property(cc.Node)
    ], GameDrag.prototype, "waterPipe", void 0);
    __decorate([
        property(cc.Node)
    ], GameDrag.prototype, "waterContainer", void 0);
    __decorate([
        property(cc.Node)
    ], GameDrag.prototype, "metaBall", void 0);
    __decorate([
        property(cc.Node)
    ], GameDrag.prototype, "ground", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDrag.prototype, "Ball", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDrag.prototype, "TopUI", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDrag.prototype, "BottomUI", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDrag.prototype, "Brick", void 0);
    __decorate([
        property({ type: cc.AudioClip })
    ], GameDrag.prototype, "waterClip", void 0);
    GameDrag = __decorate([
        ccclass
    ], GameDrag);
    return GameDrag;
}(cc.Component));
exports.default = GameDrag;

cc._RF.pop();